package com.five.bi.service.impl;

import com.five.bi.mapper.TableMapper;
import com.five.bi.model.TableModel;
import com.five.bi.model.Tableschema;
import com.five.bi.model.request.ListTableDesRequest;
import com.five.bi.model.request.ListTableRequest;
import com.five.bi.service.DataTableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DataTableServiceImpl implements DataTableService {

    @Autowired
    private TableMapper tablemapper;
    @Override
    public  List<Map<String, String>> list(ListTableRequest listTableRequest)  {
        List<Map<String, String>> list = new ArrayList<>();
        ArrayList<String> result = tablemapper.getTables_clickHouse(listTableRequest.getDbName());
        if (result!=null)
            result.forEach(tableName->
            {
                Map<String, String> map = new HashMap<String, String>();
                map.put("tableName",tableName);
                list.add(map);

            });
        return list;
    }

    @Override
    public ArrayList<Tableschema> schemalist(ListTableDesRequest listTableDesRequest) {
        ArrayList<TableModel> result = null;
        if (listTableDesRequest.getDataSourceType().equals("mysql"))
            result = tablemapper.getSchemas(listTableDesRequest.getTableName(),listTableDesRequest.getDbName());
        else if (listTableDesRequest.getDataSourceType().equals("clickhouse"))
            result = tablemapper.getSchemas_clickHouse(listTableDesRequest.getTableName(),listTableDesRequest.getDbName());
        ArrayList<Tableschema> list = new ArrayList<>();
        if (result!=null)
            result.forEach(o->
            {
                Tableschema tableschema = new Tableschema(o.getName(),o.getType(),o.getDescr(),false);
                list.add(tableschema);
            });
        return list;
    }

    @Override
    public List<Map<String, String>> listLike(ListTableDesRequest listTableDesRequest) {
        List<Map<String, String>> list = new ArrayList<>();
        ArrayList<String> result = null;
        if (listTableDesRequest.getDataSourceType().equals("mysql"))
            result = tablemapper.getTablesLike(listTableDesRequest.getTableName(),listTableDesRequest.getDbName());
        else if (listTableDesRequest.getDataSourceType().equals("clickhouse"))
            result = tablemapper.getTables_clickHouseLike(listTableDesRequest.getTableName(),listTableDesRequest.getDbName());
        if (result!=null)
            result.forEach(tableName->
            {
                Map<String, String> map = new HashMap<String, String>();
                map.put("tableName",tableName);
                list.add(map);

            });
        return list;
    }

}
